﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
namespace Things.Web.Farm
{
    public partial class Add : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUI();

                if (Request.Params["FarmerID"] != null && Request.Params["FarmerID"].Trim() != "")
                {
                    Guid farmerID = Guid.Empty;
                    Guid.TryParse(Request.Params["FarmerID"], out farmerID);
                    if (farmerID != Guid.Empty)
                    {
                        Things.Common.UICommon.SetSelectedDDL(ddlFarmer, farmerID.ToString());
                        ddlFarmer.Enabled = false;
                    }
                }
                else
                {
                    ddlFarmer.Enabled = true;
                }
            }
        }

        protected void BindUI()
        {
            Things.BLL.Farmer farmerBLL = new BLL.Farmer();
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" IsDeleted=0 ");
            ds = farmerBLL.GetList(strWhere.ToString());
            Things.Common.UICommon.BindDDL(ddlFarmer, ds, "FarmerName", "FarmerID");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.ddlFarmer.SelectedValue == "-1")
            {
                strErr += "请选择农户！\\n";
            }
            if (this.txtAddress.Text.Trim().Length == 0)
            {
                strErr += "农田地址不能为空！\\n";
            }
            if (this.txtCoordinate.Text.Trim().Length == 0)
            {
                strErr += "农田坐标不能为空！\\n";
            }

            Guid farmerID = Guid.Empty;
            try
            {
                farmerID = new Guid(this.ddlFarmer.SelectedValue);
            }
            catch
            {
                strErr += "农户信息有误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            
            string Address = this.txtAddress.Text;
            string Coordinate = this.txtCoordinate.Text;
            bool IsDeleted = false;

            Things.Model.Farm model = new Things.Model.Farm();
            model.FarmerID = farmerID;
            model.Address = Address;
            model.Coordinate = Coordinate;
            model.IsDeleted = IsDeleted;

            Things.BLL.Farm bll = new Things.BLL.Farm();
            bll.Add(model);
            Things.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "add.aspx");

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
